package com.jzqf.home.ui.screen


import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.jzqf.home.entity.bean.ItemMenuBean
import com.jzqf.home.entity.bean.menuList
import com.jzqf.home.ui.components.BannerComponent

@Composable
fun HomeScreen(onNavigateTo: (String) -> Unit) {
    Column {
        Header()
        MenuList(onNavigateTo)
    }
}

@Preview
@Composable
fun HomeScreenPreview() {
    Column {
        Header()
        MenuList {}
    }
}

@Composable
fun Header() {
    BannerComponent()
}

@Composable
fun MenuList(onNavigateTo: (String) -> Unit) {
    val menuItemList = menuList()
    LazyVerticalGrid(columns = GridCells.Adaptive(minSize = 128.dp)) {
        items(menuItemList.size) { index ->
            val itemMenuBean = menuItemList[index]
            ItemMenu(itemMenuBean, onNavigateTo)
        }
    }
}


@Composable
fun ItemMenu(itemMenuBean: ItemMenuBean, onNavigateTo: (String) -> Unit) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier
            .padding(vertical = 10.dp, horizontal = 16.dp)
            .clickable {
                onNavigateTo(itemMenuBean.route)
            },
    ) {
        Image(
            painter = painterResource(id = itemMenuBean.iconRes),
            contentDescription = itemMenuBean.name,
            modifier = Modifier.size(width = 28.dp, height = 28.dp)
        )
        Text(
            itemMenuBean.name, fontSize = 14.sp
        )
    }
}
